package com.peas.platform.module.icims.budget.apply.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.budget.apply.adapter.repository.BudgetTwoRepository;
import com.peas.platform.module.icims.budget.apply.adapter.repository.po.BudgetTwoPo;
import com.peas.platform.module.icims.budget.apply.domain.BudgetTwoDomain;
import com.peas.platform.module.icims.budget.apply.dto.*;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoFileService;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoItemService;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoKpiService;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoYearService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class BudgetTwoDomainImpl extends BaseDomainImpl<BudgetTwoDto, BudgetTwoPo> implements BudgetTwoDomain {

    @Resource
    private BudgetTwoRepository budgetTwoRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private BudgetTwoItemService budgetTwoItemService;
    @Resource
    private BudgetTwoYearService budgetTwoYearService;
    @Resource
    private BudgetTwoKpiService budgetTwoKpiService;
    @Resource
    private BudgetTwoFileService budgetTwoFileService;

    @Override
    public BaseRepository<BudgetTwoPo> getMapper() {
        return budgetTwoRepository;
    }

    @Override
    public Class<? extends BaseRepository<BudgetTwoPo>> getRepositoryClass() {
        return BudgetTwoRepository.class;
    }

    @Transactional
    @Override
    public int save(BudgetTwoDto dto) {
        if (StrUtil.isBlank(dto.getCode())) {
            String code = sequenceService.genSequence("base", "BudgetTwoDto", "BT", 4, 1);
            dto.setCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BudgetTwoDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        // items
        List<BudgetTwoItemDto> ptids = budgetTwoItemService.listByField("budgetTwoId", id);
        budgetTwoItemService.removeBatchByList(ptids);
        // years
        List<BudgetTwoYearDto> ptyds = budgetTwoYearService.listByField("budgetTwoId", id);
        budgetTwoYearService.removeBatchByList(ptyds);
        // kpis
        List<BudgetTwoKpiDto> ptkds = budgetTwoKpiService.listByField("budgetTwoId", id);
        budgetTwoKpiService.removeBatchByList(ptkds);
        // files
        List<BudgetTwoFileDto> ptfds = budgetTwoFileService.listByField("budgetTwoId", id);
        budgetTwoFileService.removeBatchByList(ptfds);
        return super.removeById(id);
    }

}
